package gl

import org.lwjgl.opengl.GL11
import org.lwjgl.opengl.GL30C

open class GLException(message: String): Exception(message)

fun checkAndClearGLException() {
    when (val error = GL11.glGetError()) {
        GL11.GL_NO_ERROR -> return
        GL11.GL_INVALID_ENUM -> System.err.println("GLWarn: GL_INVALID_ENUM")
        GL11.GL_INVALID_VALUE -> System.err.println("GLWarn: GL_INVALID_VALUE")
        GL11.GL_INVALID_OPERATION -> System.err.println("GLWarn: GL_INVALID_OPERATION")
        GL11.GL_STACK_OVERFLOW -> System.err.println("GLWarn: GL_STACK_OVERFLOW")
        GL11.GL_STACK_UNDERFLOW -> System.err.println("GLWarn: GL_STACK_UNDERFLOW")
        GL11.GL_OUT_OF_MEMORY -> System.err.println("GLWarn: GL_OUT_OF_MEMORY")
        GL30C.GL_INVALID_FRAMEBUFFER_OPERATION -> System.err.println("GLError: GL_INVALID_FRAMEBUFFER_OPERATION")
        else -> System.err.println("GLWarn: Unknown code $error(0x${Integer.toHexString(error)}")
    }
    for (element in Thread.currentThread().stackTrace) {
        System.err.println("\tat $element")
    }
}
fun checkAndThrowGLException() {
    when (val error = GL11.glGetError()) {
        GL11.GL_NO_ERROR -> return
        GL11.GL_INVALID_ENUM -> throw GLException("GLError: GL_INVALID_ENUM")
        GL11.GL_INVALID_VALUE -> throw GLException("GLError: GL_INVALID_VALUE")
        GL11.GL_INVALID_OPERATION -> throw GLException("GLError: GL_INVALID_OPERATION")
        GL11.GL_STACK_OVERFLOW -> throw GLException("GLError: GL_STACK_OVERFLOW")
        GL11.GL_STACK_UNDERFLOW -> throw GLException("GLError: GL_STACK_UNDERFLOW")
        GL11.GL_OUT_OF_MEMORY -> throw GLException("GLError: GL_OUT_OF_MEMORY")
        GL30C.GL_INVALID_FRAMEBUFFER_OPERATION -> throw GLException("GLError: GL_INVALID_FRAMEBUFFER_OPERATION")
        else -> throw GLException("GLError: Unknown code $error(0x${Integer.toHexString(error)})")
    }
}